<?php
namespace Apps\BoxEdu\Model;

use Think\Model;

class EduQuestionsModel extends \BaseModel
{
    public function load($id = "")
    {
        $data = $this->find($id);
        if (!$data) {
            return false;
        }
        $questiontype = $data['questiontype'];
        $qkquestionid = $data['questionid'];
        $data['questype'] = BoxModel("edu_questype")->where("questid='$questiontype'")->find();
        /*****单选项**/
        if ($data['questype']['questchoice'] == 1 || $data['questype']['questchoice'] == 4) {
            $data['choose_type'] = "questchoice_1";
        }
        /******多项**/
        if ($data['questype']['questchoice'] == 2 || $data['questype']['questchoice'] == 3) {
            $data['choose_type'] = "questchoice_2_3";
        }
        if ($data['questype']['questsort'] == 1 || $data['questype']['questsort'] == 3) {
            $data['choose_type'] = "questchoice_questsort_3";//主客题或冒题
        }
        $this->charactors = array('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z');
        $data['questionanswer'] = json_decode($data['questionanswer'], true);
        $showQuestionanswer = array();
        $hideQuestionanswer = array();
        if (is_array($data['questionanswer'])) {
            // dump($data['questionanswer']);
            foreach ($data['questionanswer'] as $k => $one) {
                if ($one['choose']) {
                    $showQuestionanswer[] = $this->charactors[$k - 1];
                    $hideQuestionanswer[] = $k;
                }
            }
        }
        $data['showQuestionanswer'] = implode("、", $showQuestionanswer);
        $data['hideQuestionanswer'] = implode("", $hideQuestionanswer);
        $data['quest2knows'] = BoxModel("edu_quest2knows")->where("qkquestionid='$qkquestionid'")->select();
        return $data;
    }

    /*********题库********/
    public function get($where, $limit = "50", $orderBy = "questionid desc")
    {
        $count = $this->alias("Q")->join("left join edu_quest2knows as K on K.qkquestionid=Q.questionid")->where($where)->count();
        $page = setPager($count, $limit);
        $limit = $page['firstRow'] . "," . $page['listRows'];
        $data = $this->where($where)->alias("Q")->join("left join edu_quest2knows as K on K.qkquestionid=Q.questionid")->field("distinct(questionid)")->limit($limit)->order($orderBy)->select();
        if ($data) {
            foreach ($data as $one) {
                $re[] = $this->load($one['questionid']);
            }
        }
        if ($data) {
            $data = array(
                'count' => $count,
                'list' => $re,
                'pager' => $page,
            );
        }
        return $data;
    }

    /******题型***/
    function questype()
    {
        $data = BoxModel("edu_questype")->where("is_show=1")->select();
        return $data;
    }


}
 